package com.hundsun.yr.universal.library.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.StateSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.View.MeasureSpec;

import com.hundsun.yr.universal.library.R;


/**
 * @Title: HSSegmentControl.java 
 * @Description: 分段选择器
 * @date 2015-12-29 上午9:45:14 
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com 
 * @replace author:
 * @replace date:
 * @version V1.0   
 */
@SuppressLint("ClickableViewAccessibility") 
@SuppressWarnings("unused")
public class HSSegmentControl extends View {

    private String[] mTexts;
    private Rect[] mCacheBounds;
    private Rect[] mTextBounds;

    private RadiusDrawable mBackgroundDrawable;
    private RadiusDrawable mSelectedDrawable;

    private int mCurrentIndex;

    private int mTouchSlop;
    private boolean inTapRegion;
    private float mStartX;
    private float mStartY;
    private float mCurrentX;
    private float mCurrentY;

    private int mHorizonGap;
    private int mVerticalGap;

    private int mCenterX;
    private int mCenterY;

	private int mChildrenWidth;
    private int mChildrenHeight;

    private int mSingleChildWidth;
    private int mSingleChildHeight;
    
    /**是否填充(默认2等分，false自适应)**/
    private boolean fixed=true;

    private Paint mPaint;

    public enum Direction{
        HORIZON(0), VERTICAL(1);

        int mV;

        private Direction(int v){
            mV = v;
        }
    }

    private Direction mDirection;

    private int mTextSize;
    private ColorStateList mColors;
    private int mCornerRadius;

    public interface OnSegmentControlClickListener{
        public void onSegmentControlClick(int index);
    }

    private OnSegmentControlClickListener mOnSegmentControlClickListener;

    public HSSegmentControl(Context context){
        this(context, null);
    }

    public HSSegmentControl(Context context,AttributeSet attrs){
        this(context, attrs, 0);
    }

    @SuppressLint("NewApi") @SuppressWarnings("deprecation")
	public HSSegmentControl(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HSSegmentControl);

        String textArray = ta.getString(R.styleable.HSSegmentControl_texts);

        int id =ta.getResourceId(R.styleable.HSSegmentControl_texts, 0);
//        TypedArray a=context.getResources().obtainTypedArray(id); 
        mTexts=context.getResources().getStringArray(id);;
//        mTexts=(String[]) ta.getTextArray(R.styleable.HSSegmentControl_texts);
//        if(textArray != null){
//            mTexts = textArray.split("\\|");
//        }


        mTextSize = ta.getDimensionPixelSize(R.styleable.HSSegmentControl_android_textSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, context.getResources().getDisplayMetrics()));
        mColors = ta.getColorStateList(R.styleable.HSSegmentControl_colors);
        mCornerRadius = ta.getDimensionPixelSize(R.styleable.HSSegmentControl_cornerRadius, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, context.getResources().getDisplayMetrics()));
        mDirection = Direction.values()[ta.getInt(R.styleable.HSSegmentControl_direction, 0)];

        mHorizonGap = ta.getDimensionPixelSize(R.styleable.HSSegmentControl_horizonGap, 0);
        mVerticalGap = ta.getDimensionPixelSize(R.styleable.HSSegmentControl_verticalGap, 0);

        fixed=ta.getBoolean(R.styleable.HSSegmentControl_fixed, true);
        
        int gap = ta.getDimensionPixelSize(R.styleable.HSSegmentControl_gaps, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, context.getResources().getDisplayMetrics()));

        if(mHorizonGap == 0) mHorizonGap = gap;
        if(mVerticalGap == 0) mVerticalGap = gap;

        ta.recycle();

        mBackgroundDrawable = new RadiusDrawable(mCornerRadius, true, 0);
        mBackgroundDrawable.setStrokeWidth(2);

        if(mColors == null){
            mColors = new ColorStateList(new int[][]{{}}, new int[]{0xFF0099CC});
        }

        mBackgroundDrawable.setStrokeColor(mColors.getDefaultColor());

        if(Build.VERSION.SDK_INT < 16){
            setBackgroundDrawable(mBackgroundDrawable);
        }else{
            setBackground(mBackgroundDrawable);
        }

        mSelectedDrawable = new RadiusDrawable(mCornerRadius, false, mColors.getDefaultColor());

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mColors.getDefaultColor());

        //here's the tricky thing, when you doing a click detect on a capacitive touch screen,
        //sometimes the touch points of touchDown and touchUp are different(it's call slop) even when you didn't actually move your finger,
        //so we set a distance limit for the distance of this two touch points to create a better user experience;
        int touchSlop = 0;

        if(context == null){
            touchSlop = ViewConfiguration.getTouchSlop();
        }else{
            final ViewConfiguration config = ViewConfiguration.get(context);
            touchSlop = config.getScaledTouchSlop();
        }

        mTouchSlop = touchSlop * touchSlop;
        inTapRegion = false;
    }

    public void setOnSegmentControlClickListener(OnSegmentControlClickListener onSegmentControlClickListener){
        mOnSegmentControlClickListener = onSegmentControlClickListener;
    }

    public OnSegmentControlClickListener getOnSegmentControlClicklistener(){
        return mOnSegmentControlClickListener;
    }

    public void setText(String... texts){
        mTexts = texts;

        if(mTexts != null){
            requestLayout();
        }
    }

    public void setColors(ColorStateList colors){
        mColors = colors;

        if(mBackgroundDrawable != null){
            mBackgroundDrawable.setStrokeColor(colors.getDefaultColor());
        }

        if(mSelectedDrawable != null){
            mSelectedDrawable.setColor(colors.getDefaultColor());
        }

        mPaint.setColor(colors.getDefaultColor());

        invalidate();
    }

    public void setCornerRadius(int cornerRadius){
        mCornerRadius = cornerRadius;

        if(mBackgroundDrawable != null){
            mBackgroundDrawable.setRadius(cornerRadius);
        }

        invalidate();
    }

    public void setDirection(Direction direction){
        Direction tDirection = mDirection;

        mDirection = direction;

        if(tDirection != direction){
            requestLayout();
            invalidate();
        }
    }

    public void setTextSize(int textSize){
        setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    }

    public void setTextSize(int unit, int textSize){
        mPaint.setTextSize((int) (TypedValue.applyDimension(unit, textSize, getContext().getResources().getDisplayMetrics())));

        if(textSize != mTextSize){
            mTextSize = textSize;
            requestLayout();
        }
    }

    @SuppressLint("DrawAllocation") @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
    	
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width = 0;
        int height = 0;

        if(mTexts != null && mTexts.length > 0){

            if(mCacheBounds == null || mCacheBounds.length != mTexts.length){
                mCacheBounds = new Rect[mTexts.length];
            }

            if(mTextBounds == null || mTextBounds.length != mTexts.length){
                mTextBounds = new Rect[mTexts.length];
            }

            for(int i = 0; i < mTexts.length; i++){
                String text = mTexts[i];

                if(text != null){

                    if(mTextBounds[i] == null) mTextBounds[i] = new Rect();

                    mPaint.getTextBounds(text, 0, text.length(), mTextBounds[i]);
                    //aa
                    if(fixed){
                    	if(mSingleChildWidth < mTextBounds[i].width() + mHorizonGap * 2) mSingleChildWidth = widthSize/mTexts.length;
                    }else{
                    	if(mSingleChildWidth < mTextBounds[i].width() + mHorizonGap * 2) mSingleChildWidth = mTextBounds[i].width() + mHorizonGap * 2;
                    }
                    
                    if(mSingleChildHeight < mTextBounds[i].height() + mVerticalGap * 2) mSingleChildHeight = mTextBounds[i].height() + mVerticalGap * 2;
                }
            }
            
            for(int i = 0; i < mTexts.length; i++){

                if (mCacheBounds[i] == null) mCacheBounds[i] = new Rect();

                if(mDirection == Direction.HORIZON){
                    mCacheBounds[i].left = i * mSingleChildWidth;
                    mCacheBounds[i].top = 0;
                }else{
                    mCacheBounds[i].left = 0;
                    mCacheBounds[i].top = i * mSingleChildHeight;
                }

                mCacheBounds[i].right = mCacheBounds[i].left + mSingleChildWidth;
                mCacheBounds[i].bottom = mCacheBounds[i].top + mSingleChildHeight;
            }

            switch(widthMode){
                case MeasureSpec.AT_MOST:
                    if(mDirection == Direction.HORIZON){
                        if(widthSize <= mSingleChildWidth * mTexts.length){
                            mSingleChildWidth = widthSize / mTexts.length;
                            width = widthSize;
                        }else{
                            width = mSingleChildWidth * mTexts.length;
                        }
                    }else{
                        width = widthSize <= mSingleChildWidth ? widthSize : mSingleChildWidth;
                    }
                    break;
                case MeasureSpec.EXACTLY:
                    width = widthSize;
                    break;
                case MeasureSpec.UNSPECIFIED:
                    if(mDirection == Direction.HORIZON){
                        width = mSingleChildWidth * mTexts.length;
                    }else{
                        width = widthSize <= mSingleChildWidth ? widthSize : mSingleChildWidth;
                    }
                    break;
            }

            switch(heightMode){
                case MeasureSpec.AT_MOST:
                    if(mDirection == Direction.VERTICAL){
                        if(heightSize <= mSingleChildHeight * mTexts.length){
                            mSingleChildHeight = heightSize / mTexts.length;
                            height = heightSize;
                        }else{
                            height = mSingleChildHeight * mTexts.length;
                        }
                    }else{
                        height = heightSize <= mSingleChildHeight ? heightSize : mSingleChildHeight;
                    }
                    break;
                case MeasureSpec.EXACTLY:
                    height = heightSize;
                    break;
                case MeasureSpec.UNSPECIFIED:
                    if(mDirection == Direction.VERTICAL){
                        height = mSingleChildHeight * mTexts.length;
                    }else{
                        height = heightSize <= mSingleChildHeight ? heightSize : mSingleChildHeight;
                    }
                    break;
            }

            mChildrenWidth = mDirection == Direction.HORIZON ? mSingleChildWidth * mTexts.length : mSingleChildWidth;
            mChildrenHeight = mDirection == Direction.VERTICAL ? mSingleChildHeight * mTexts.length : mSingleChildHeight;
        }else{
            width = widthMode == MeasureSpec.UNSPECIFIED ? 0 : widthSize;
            height = heightMode == MeasureSpec.UNSPECIFIED ? 0 : heightSize;
        }

        mCenterX = width / 2;
        mCenterY = height / 2;

        setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){

        switch(event.getAction() & MotionEvent.ACTION_MASK){
            case MotionEvent.ACTION_DOWN:
                inTapRegion = true;

                mStartX = event.getX();
                mStartY = event.getY();

                break;
            case MotionEvent.ACTION_MOVE:
                mCurrentX = event.getX();
                mCurrentY = event.getY();

                int dx = (int) (mCurrentX - mStartX);
                int dy = (int) (mCurrentY - mStartY);

                int distance = dx * dx + dy * dy;

                if(distance > mTouchSlop){
                    inTapRegion = false;
                }

                break;
            case MotionEvent.ACTION_UP:
                if(inTapRegion){
                    int index = 0;
                    if(mDirection == Direction.HORIZON){
                        index = (int) (mStartX / mSingleChildWidth);
                    }else{
                        index = (int) (mStartY / mSingleChildHeight);
                    }

                    if(mOnSegmentControlClickListener != null) mOnSegmentControlClickListener.onSegmentControlClick(index);

                    mCurrentIndex = index;

                    invalidate();
                }
                break;
        }

        return true;
    }

    public void setSelectedIndex(int index){
        mCurrentIndex = index;
        if(mOnSegmentControlClickListener!=null){
        	mOnSegmentControlClickListener.onSegmentControlClick(index);
        }
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);

        if(mTexts != null && mTexts.length > 0){
            for(int i = 0; i < mTexts.length; i++){

                //draw separate lines
                if(i < mTexts.length - 1){
                    mPaint.setColor(mColors.getDefaultColor());
                    if(mDirection == Direction.HORIZON){
                        canvas.drawLine(mCacheBounds[i].right, 0, mCacheBounds[i].right, getHeight(), mPaint);
                    }else{
                        canvas.drawLine(mCacheBounds[i].left, mSingleChildHeight * (i + 1), mCacheBounds[i].right, mSingleChildHeight * (i + 1), mPaint);
                    }
                }

                //draw selected drawable
                if(i == mCurrentIndex && mSelectedDrawable != null){
                    int topLeftRadius = 0;
                    int topRightRadius = 0;
                    int bottomLeftRadius = 0;
                    int bottomRightRadius = 0;

                    if(mDirection == Direction.HORIZON){
                        if(i == 0){
                            topLeftRadius = mCornerRadius;
                            bottomLeftRadius = mCornerRadius;
                        }else if(i == mTexts.length - 1){
                            topRightRadius = mCornerRadius;
                            bottomRightRadius = mCornerRadius;
                        }
                    }else{
                        if(i == 0){
                            topLeftRadius = mCornerRadius;
                            topRightRadius = mCornerRadius;
                        }else if(i == mTexts.length - 1){
                            bottomLeftRadius = mCornerRadius;
                            bottomRightRadius = mCornerRadius;
                        }
                    }

                    mSelectedDrawable.setRadiuses(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
                    mSelectedDrawable.setBounds(mCacheBounds[i]);
                    mSelectedDrawable.draw(canvas);

                    if(mColors.isStateful()){
                    	mPaint.setColor(mColors.getColorForState(View.FOCUSED_SELECTED_STATE_SET, 0xFFFFFFFF));
                    }else{
                    	
                    	mPaint.setColor(0xFFFFFFFF);
                    }
                    if(mColors.getColorForState(View.FOCUSED_SELECTED_STATE_SET, 0xFFFFFFFF)!=0xFFFFFFFF){
                    	
                    }
                }else{
                    mPaint.setColor(mColors.getDefaultColor());
                }

                //draw texts
//                canvas.drawText(mTexts[i], mCacheBounds[i].left + (mSingleChildWidth - mTextBounds[i].width()) / 2, mCacheBounds[i].top + ((mSingleChildHeight + mTextBounds[i].height()) / 2), mPaint);
                //draw texts  字体居中方案
                canvas.drawText(mTexts[i], mCacheBounds[i].left + (mSingleChildWidth - mTextBounds[i].width()) / 2, 
                		(mCacheBounds[i].top+mCacheBounds[i].bottom)/2 -((mPaint.descent() + mPaint.ascent()) / 2), mPaint);
            }
        }

    }
    
    
    /**
     * @Title: RadiusDrawable.java 
     * @Description: 圆角 Drawable 每个角的圆半径可以不同
     * @date 2015-12-29 上午9:41:46 
     * @author:hejun.shen
     * @email:shenhj15971@hundsun.com 
     * @replace author:
     * @replace date:
     * @version V1.0   
     */
    public class RadiusDrawable extends Drawable {

        private int topLeftRadius;
        private int topRightRadius;
        private int bottomLeftRadius;
        private int bottomRightRadius;

        private int left;
        private int top;
        private int right;
        private int bottom;

        private int width;
        private int height;

        private int centerX;
        private int centerY;

        private final Paint paint;
        private int color;
        private final boolean isStroke;
        private int strokeWidth = 0;
        private int strokeColor;

        private Path path;

        public RadiusDrawable(int topLeftRadius, int topRightRadius, int bottomLeftRadius,
                              int bottomRightRadius, boolean isStroke, int color) {
            this.topLeftRadius = topLeftRadius;
            this.topRightRadius = topRightRadius;
            this.bottomLeftRadius = bottomLeftRadius;
            this.bottomRightRadius = bottomRightRadius;

            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            this.isStroke = isStroke;
            this.color = color;
        }

        public RadiusDrawable(int radius, boolean isStroke, int color) {
            this.topLeftRadius =
                    this.topRightRadius = this.bottomLeftRadius = this.bottomRightRadius = radius;

            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            this.isStroke = isStroke;
            this.color = color;

        }

        public void setStrokeWidth(int width) {
            strokeWidth = width;
            setBounds(left, top, right, bottom);
        }

        public void setStrokeColor(int strokeColor) {
            this.strokeColor = strokeColor;
        }

        public void setColor(int color){
            this.color = color;
        }

        public void setRadius(int radius){
            this.topLeftRadius = this.topRightRadius = this.bottomLeftRadius = this.bottomRightRadius = radius;
        }

        public void setRadiuses(int topLeftRadius, int topRightRadius, int bottomLeftRadius, int bottomRightRadius){
            this.topLeftRadius = topLeftRadius;
            this.topRightRadius = topRightRadius;
            this.bottomLeftRadius = bottomLeftRadius;
            this.bottomRightRadius = bottomRightRadius;
        }

        @Override
        public void setBounds(int left, int top, int right, int bottom) {
            super.setBounds(left, top, right, bottom);

            this.left = left;
            this.top = top;
            this.right = right;
            this.bottom = bottom;

            if (isStroke) {
                int halfStrokeWidth = strokeWidth / 2;
                left += halfStrokeWidth;
                top += halfStrokeWidth;
                right -= halfStrokeWidth;
                bottom -= halfStrokeWidth;
            }

            path = new Path();
            path.moveTo(left + topLeftRadius, top);
            path.lineTo(right - topRightRadius, top);
            path.arcTo(new RectF(right - topRightRadius * 2, top, right, top + topRightRadius * 2),
                    -90, 90);
            // path.quadTo(right, top, right, top + topRightRadius);
            path.lineTo(right, bottom - bottomRightRadius);
            path.arcTo(new RectF(right - bottomRightRadius * 2, bottom - bottomRightRadius * 2, right,
                    bottom), 0, 90);
            // path.quadTo(right, bottom, right - bottomRightRadius, bottom);
            path.lineTo(left + bottomLeftRadius, bottom);
            path.arcTo(new RectF(left, bottom - bottomLeftRadius * 2, left + bottomLeftRadius * 2,
                    bottom), 90, 90);
            // path.quadTo(left, bottom, left, bottom - bottomLeftRadius);
            path.lineTo(left, top + topLeftRadius);
            path.arcTo(new RectF(left, top, left + topLeftRadius * 2, top + topLeftRadius * 2), 180, 90);
            // path.quadTo(left, top, left + topLeftRadius, top);
            path.close();
        }

        @Override
        public void draw(Canvas canvas) {
            if (color != 0) {
                paint.setColor(color);
                paint.setStyle(Paint.Style.FILL);
                canvas.drawPath(path, paint);
            }

            if (strokeWidth > 0) {
                paint.setColor(strokeColor);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeJoin(Paint.Join.MITER);
                paint.setStrokeWidth(strokeWidth);
                canvas.drawPath(path, paint);
            }
        }

        @Override
        public void setAlpha(int alpha) {

        }

        @Override
        public void setColorFilter(ColorFilter cf) {

        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

    }
}
